<template>
  <el-container class="home-container">
    <!--头部区域-->
    <el-header>
      <div>
        <img src="../assets/heima.png">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出登录</el-button>
    </el-header>
    <!--页面主体区域-->
    <el-container>
      <!--左侧边栏-->
      <el-aside :width="asideWidth">
        <!--折叠/展开的按钮条-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu :default-active="activePath" router :collapse="isCollapse" :collapse-transition="false" unique-opened background-color="#333744" text-color="#fff" active-text-color="#409EFF">
          <!--一级菜单-->
          <el-submenu :index="item.id+''" :key="item.id" v-for="item in menuList">
            <!--一级菜单的模板区-->
            <template slot="title">
              <!--图标-->
              <i :class="iconsObj[item.id]"></i>
              <!--文本-->
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item :index="'/'+subitem.path" :key="subitem.id" v-for="subitem in item.children" @click="saveNavState('/'+subitem.path)">
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--文本-->
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧主体区-->
      <el-main>
        <!--welcome组件的路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 是否折叠  默认false
      isCollapse: false,
      // 定义菜单列表数据
      menuList: [],
      // 自定义一级菜单图标
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    // 页面一加载 获取左侧菜单栏的列表数据
    this.getMenuList()
    //
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      // 清空session
      window.sessionStorage.clear()
      // 编程式导航到登录页
      this.$router.push('/login')
    },
    async getMenuList () {
      // 发送请求  获取后台数据  得到的数据是被axios包装过的
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      // 如果请求失败
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 如果请求成功
      this.menuList = res.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存当前导航菜单的激活状态
    saveNavState (activePath) {
      // 存储在sessionStorage中
      window.sessionStorage.setItem('activePath', activePath)
      //
      this.activePath = activePath
    }
  },
  computed: {
    asideWidth () {
      if (this.isCollapse) {
        // 如果是折叠的  返回64px
        return '64px'
      } else {
        // 如果未折叠 返回200px
        return '200px'
      }
    }
  }
}
</script>

<style lang='less' scoped>
.home-container {
  height: 100%;
}
.el-header {
  /*felx布局*/
  display: flex;
  /*两边贴边对齐*/
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  background-color: #373D41;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #EAEDF1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4A5064;
  text-align: center;
  color: #fff;
  line-height: 24px;
  font-size: 10px;
  letter-spacing: .2em;
  cursor: pointer;
}
</style>
